from django.db import models
from django.db.models.signals import post_migrate
from django.dispatch import receiver
from django.contrib.auth import get_user_model
from django.core.management import call_command


@receiver(post_migrate)
def create_superuser(sender, **kwargs):
    """在数据库迁移后创建超级用户"""
    User = get_user_model()

    # 检查是否已存在admin用户
    if not User.objects.filter(username='admin').exists():
        # 创建超级用户
        User.objects.create_superuser(
            username='admin',
            email='admin@example.com',
            password='admin123'
        )
        print('超级用户 "admin" 已创建，密码为 "admin123"')
    else:
        # 如果admin用户已存在，确保密码为admin123
        admin_user = User.objects.get(username='admin')
        admin_user.set_password('admin123')
        admin_user.save()
        print('超级用户 "admin" 已存在，密码已更新为 "admin123"')
